package cn.sirun.com.fca.account.presenter.impl;


import com.tencent.cloud.iov.kernel.model.BaseResponse;

import cn.sirun.com.fca.account.constant.AccountActionConst;
import cn.sirun.com.fca.account.loader.AuthenticationLoader;
import cn.sirun.com.fca.account.model.bind.RecognizePicRequest;
import cn.sirun.com.fca.account.model.bind.RecognizePicResponse;
import cn.sirun.com.fca.account.model.bind.SubmitOwnerInfoRequest;
import cn.sirun.com.fca.account.observer.CommonExceptionObserver;
import cn.sirun.com.fca.account.presenter.AccountFlowPresenter;
import cn.sirun.com.fca.account.presenter.IAuthenticationPresenter;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.subjects.BehaviorSubject;

/**
 * @author vinsonhuang
 * @date 2018/5/4
 */
public class AuthenticationPresenter extends AccountFlowPresenter<AuthenticationLoader>
        implements IAuthenticationPresenter {

    private BehaviorSubject<BaseResponse> mBehaviorSubject = BehaviorSubject.create();

    @Override
    protected AuthenticationLoader createLoader() {
        return new AuthenticationLoader();
    }

    @Override
    public void recognizePic(final RecognizePicRequest request) {
        getLoader().recognizePic(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonExceptionObserver<RecognizePicResponse>(this) {
                    @Override
                    public void onNext(BaseResponse<RecognizePicResponse> response) {
                        super.onNext(response);
                        onAction(AccountActionConst.Normal.ACTION_RECOGNIZE_PIC_RESULT, response);
                    }
                });
    }


    @Override
    public void submitOwnerInfo(SubmitOwnerInfoRequest request) {
        getLoader().submitOwnerInfo(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonExceptionObserver<Void>(this) {
                    @Override
                    public void onNext(BaseResponse<Void> response) {
                        super.onNext(response);
                        onAction(AccountActionConst.Normal.ACTION_SUBMIT_OWNER_INFO_RESULT,
                                response);
                    }
                });
    }


    @Override
    public void submitOwnerImg(SubmitOwnerInfoRequest request) {
        getLoader().submitOwnerImg(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonExceptionObserver<RecognizePicResponse>(this) {
                    @Override
                    public void onNext(BaseResponse<RecognizePicResponse> response) {
                        super.onNext(response);
                        onAction(AccountActionConst.Normal.ACTION_SUBMIT_OWNER_IMG_RESULT, response);
                    }
                });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mBehaviorSubject.onComplete();
    }
}
